/*
 * @Author: your name
 * @Date: 2021-11-22 16:02:33
 * @LastEditTime: 2022-01-06 21:14:29
 * @LastEditors: your name
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \agricultural-production-web\src\api\axios.js
 */
/*
*@descrion:此封装方法主要时用户的登录，与选择农场页面
*/
import axios from "axios";
// import { Message } from "element-ui";
import { getToken } from "@/utils/auth";
import store from "@/store";
import baseUrl from "@/api/baseURL"
const zhaxios = axios.create({
  // baseURL: "http://ia.senontech.com/",

  baseURL: baseUrl.webDocumentUrl,
  // baseURL: "http://192.168.1.85:9090/production/",
  // baseURL: "http://192.168.1.97:9090/",
  timeout: 40000 // 请求超时时间
});

// request interceptor
zhaxios.interceptors.request.use(
  config => {
    // 全局参数 end

    config.cancelToken = new axios.CancelToken(cancel => {
      store.commit('permission/add_AxiosPromiseArr', { cancel })
    })
    return config;
  },
  error => {
    console.log(error);
    return Promise.reject(error);
  }
);

// response interceptor
zhaxios.interceptors.response.use(
  response => {
    const res = response.data;
    return res;
  },
  error => {
    console.log("err" + error);
    // Message({
    //   message: error.message,
    //   type: "error",
    //   duration: 5 * 1000
    // });
    return Promise.reject(error);
  }
);

// 封装get请求
export function get(url, params = {}) {
  params.t = new Date().getTime(); // get方法加一个时间参数,解决ie下可能缓存问题.
  console.log(url, 'get', params)
  return zhaxios({
    url: url,
    method: "get",
    params
  });
}

// 封装post请求
export function post(url, data = {}) {
  // 默认配置
  let sendObject = {
    url: url,
    method: "post",
    data
  };
  console.log(url, 'post', data)

  sendObject.data = JSON.stringify(data);
  return zhaxios(sendObject);
}

export default zhaxios;
